package com.zx.base.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.zx.tv.MusicReceiver;
import com.zx.tv.app.NewTVApp;



public class UpdateManager {

	//返回的安装包url
	private String apkUrl = "";
	
	 /* 下载包安装路径 */
    private static final String savePath = "/sdcard/newpai/";
    private String savePath_down = "music/";
    private String mSnap = "snap.mp3";
    private String mSaveName = "newpaitv.mp3";
    private String mSnapFileName = savePath + savePath_down + mSnap;
    private String mSaveFileName = savePath + savePath_down + mSaveName;

    private static final int DOWN_UPDATE = 1;
    
    private static final int DOWN_OVER = 2;
    
    private Thread downLoadThread;
    
    private boolean interceptFlag = false;
    private NewTVApp app;
    
    
    private Handler mHandler = new Handler(){
    	public void handleMessage(Message msg) {
    		switch (msg.what) {
			case DOWN_UPDATE:
				break;
			case DOWN_OVER:
				

				saveID();
				break;
			case 3:
				
				break;
			default:
				break;
			}
    	};
    };
    
	public UpdateManager(NewTVApp app, String url) {
		this.apkUrl = url;
		this.app = app;
	}
	
	
	private Runnable mdownApkRunnable = new Runnable() {	
		@Override
		public void run() {
			try {
				URL url = new URL(apkUrl);
			
				HttpURLConnection conn = (HttpURLConnection)url.openConnection();
				conn.setConnectTimeout(3000);
				conn.setReadTimeout(30000);
				conn.setRequestMethod("GET");
				conn.connect();
				InputStream is = conn.getInputStream();

				File file = new File(savePath);
				if(!file.exists()){
					file.mkdir();
				}
				
				File file1 = new File(savePath + savePath_down);
				if(!file1.exists()){
					file1.mkdir();
				}
				
				String apkFile = mSnapFileName;
				File ApkFile = new File(apkFile);
				FileOutputStream fos = new FileOutputStream(ApkFile);
				
				byte buf[] = new byte[1024];
				
				do{   		   		
		    		int numread = is.read(buf);
		    		if(numread <= 0){	
		    			//下载完成通知安装
		    			mHandler.sendEmptyMessage(DOWN_OVER);
		    			break;
		    		}
		    		fos.write(buf,0,numread);
		    	}while(!interceptFlag);//点击取消就停止下载.
				
				fos.close();
				is.close();
			} catch (Exception e) {
				
				mHandler.sendEmptyMessage(3);
				e.printStackTrace();
			}
			
		}
	};
	
	 /**
     * 下载apk
     * @param url
     */
	
	public void downloadMp3(){
		downLoadThread = new Thread(mdownApkRunnable);
		downLoadThread.start();
	}
	
	private void saveID(){
		File file = new File(mSnapFileName);
		file.renameTo(new File(mSaveFileName));
		
		Intent intent = new Intent(app.getApplicationContext(), MusicReceiver.class); 
        
        Bundle bundle = new Bundle();  
        bundle.putInt("op", 8);  
        intent.putExtras(bundle);  
  
        app.getApplicationContext().sendBroadcast(intent);
		
		SharedPreferences sp = app.getSharedPreferences("musicid", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		editor.putString("id", app.music.getId());
		editor.commit();
		
	}

}
